﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace GadgetBase
{
    public static class GlobalMethods
    {
        public static string GetFullPath(string path)
        {
            Assembly assembly = Assembly.GetCallingAssembly();
            string executableDirectory = Path.GetDirectoryName(assembly.Location);
            return Path.Combine(executableDirectory, path);            
        }

        public static BitmapImage GetBitmapImage(string fullPath)
        {
            if (!File.Exists(fullPath))
            {
                string message = string.Format("Image \"{0}\" not found.\nFull path: \"{1}\"", System.IO.Path.GetFileName(fullPath), fullPath);
#if DEBUG                
                ShowErrorMessageBox(message);
#endif
                throw new FileNotFoundException(message);
            }            
            return new BitmapImage(new Uri(fullPath, UriKind.RelativeOrAbsolute));
        }

        public static DialogResult ShowErrorMessageBox(string message)
        {
            return MessageBox.Show(message, "Error...", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static Image GetImage(string localPath)
        {
            Image image = new Image();
            image.Source = GlobalMethods.GetBitmapImage(localPath);
            image.Width = image.Source.Width;
            image.Height = image.Source.Height;
            image.Stretch = Stretch.Fill;
            return image;
        }

    }

    public static class Utility
    {
        [DllImport("kernel32.dll")]
        public static extern void GlobalMemoryStatus(out MemoryStatus stat);

        public struct MemoryStatus
        {

            public uint Length; //Length of struct
            public uint MemoryLoad; //Value from 0-100 represents memory usage
            public uint TotalPhysical;
            public uint AvailablePhysical;
            public uint TotalPageFile;
            public uint AvailablePageFile;
            public uint TotalVirtual;
            public uint AvailableVirtual;

            public void Update()
            {
                GlobalMemoryStatus(out this);
            }
        }

        public static MemoryStatus GetGlobalMemoryStatus()
        {
            MemoryStatus result = new MemoryStatus();

            GlobalMemoryStatus(out result);
            
            return result;
        }
    }
}
